package aorise.com.frame.ui;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bm.library.PhotoView;
import com.wang.avi.AVLoadingIndicatorView;

import aorise.com.frame.R;
import aorise.com.frame.base.BBBaseActivity;
import aorise.com.frame.common.FileUtils;
import aorise.com.frame.common.CommonUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CommonOpenPictureActivity extends BBBaseActivity {

    @BindView(R.id.img)
    PhotoView img;
    @BindView(R.id.txt_save)
    TextView txtSave;
    @BindView(R.id.avi)
    AVLoadingIndicatorView avi;
    private String pictureUrl;
    @DrawableRes
    private int errorPicture = R.drawable.glide_idcard_p_load_fail;
    private int placeHolder = R.drawable.glide_idcard_p_loading;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_common_open_picture);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        pictureUrl = getIntent().getStringExtra("url");
        errorPicture = getIntent().getIntExtra("error",R.drawable.glide_idcard_p_load_fail);
        placeHolder = getIntent().getIntExtra("placeholder",R.drawable.glide_idcard_p_loading);

        //添加_max展示原图
//        int lastIndexOf = pictureUrl.lastIndexOf(".");
//        String begin = pictureUrl.substring(0, lastIndexOf);
//        String end = pictureUrl.substring(lastIndexOf, pictureUrl.length());
//        pictureUrl=begin+"_max"+end;
//        System.out.println("sub:"+pictureUrl);
    }

    @Override
    protected void initEvent() {
        CommonUtil.loadImage(this, img, pictureUrl, placeHolder, errorPicture);
        img.enable();
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonOpenPictureActivity.this.finish();


            }
        });
    }


    @OnClick(R.id.txt_save)
    public void onViewClicked() {
        Bitmap image = ((BitmapDrawable) img.getDrawable()).getBitmap();
        avi.show();
        txtSave.setVisibility(View.GONE);
        avi.setVisibility(View.VISIBLE);
        FileUtils.savePhoto(this, image, new FileUtils.SaveResultCallback() {
            @Override
            public void onSavedSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(CommonOpenPictureActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                        avi.hide();
                        avi.setVisibility(View.GONE);
                        txtSave.setVisibility(View.VISIBLE);
                    }
                });
            }

            @Override
            public void onSavedFailed() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(CommonOpenPictureActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                        avi.hide();
                        avi.setVisibility(View.GONE);
                        txtSave.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
    }

}
